package com.dmall.chapter03.shizhan;

public class Leetcode206 {

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode prev = cur;
            cur = cur.next;
            prev.next = next;
            next = prev;
        }
        return next;
    }

    /*public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }*/

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode next = head;
        for (int i = 2; i <= 5; i++) {
            next.next = new ListNode(i);
            next = next.next;
        }

        Leetcode206 leetcode206 = new Leetcode206();
        head = leetcode206.reverseList(head);

        StringBuilder sb = new StringBuilder();
        while (head != null) {
            sb.append(head.val).append("->");
            head = head.next;
        }
        sb.append("NULL");
        System.out.println(sb.toString());
    }
}
